package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.DateUtil;
import ysq.xyg.web.dao.WebArticleDao;
import ysq.xyg.web.model.po.WebArticle;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.po.WebTagContent;
import ysq.xyg.web.model.vo.WebArticleVo;
import ysq.xyg.web.service.WebArticleService;
import ysq.xyg.web.service.WebTagContentService;
import ysq.xyg.web.util.WebBaseContent;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 网站模块-网站文档service实现
 *
 * @author shengQiang yu
 * @date 2019-6-5 21:56:08
 */
@Service("webArticleService")
public class WebArticleServiceImpl extends BaseServiceImpl<WebArticle> implements WebArticleService {

    @Resource
    private WebArticleDao webArticleDao;
    @Resource
    private WebTagContentService webTagContentService;
    private WebArticle webArticle;

    @Autowired
    public void setBaseDao(WebArticleDao webArticleDao) {
        this.baseDao = webArticleDao;
    }

    /**
     * 通过栏目获取信息
     *
     * @param webArticleVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-5 21:56:08
     */
    public ReturnEntity pageByCol(WebArticleVo webArticleVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<WebArticle> list = webArticleDao.listLeftByCol(webArticleVo);
        PageInfo<WebArticle> page=new PageInfo<WebArticle>(list);
        if(page.getList()!=null&&page.getList().size()>0){

        }
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }
    /**
     * 分页查询
     *
     * @param webArticleVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-5 21:56:08
     */
    public ReturnEntity page(WebArticleVo webArticleVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<WebArticle> list = webArticleDao.listLeft(webArticleVo);
        PageInfo<WebArticle> page=new PageInfo<WebArticle>(list);
        if(page!=null&&page.getList().size()>0){
            for (WebArticle webArticle:page.getList()) {
                webArticle.setCreateTimeStr(DateUtil.getDateText(webArticle.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
            }
        }
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webArticleVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-5 21:56:08
     */
    public ReturnEntity addOrUpdate(WebArticleVo webArticleVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webArticleVo.getId())){
            webArticle=new WebArticle(currentUser);
            isUpate=false;
        }else{
            webArticle=selectByPrimaryKey(webArticleVo.getId());
            if(ObjectUtil.isNull(webArticle)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webArticle.setUpdateTime(new Date());
            webArticle.setUpdateUser(currentUser.getId());
        }
        webArticle.setTitle(webArticleVo.getTitle());
        webArticle.setColId(webArticleVo.getColId());
        webArticle.setAuthor(webArticleVo.getAuthor());
        webArticle.setThumImage(webArticleVo.getThumImage());
        webArticle.setCoverImage(webArticleVo.getCoverImage());
        webArticle.setSeoKeyword(webArticleVo.getSeoKeyword());
        webArticle.setDescription(webArticleVo.getDescription());
        webArticle.setContent(webArticleVo.getContent());
        webArticle.setSort(webArticleVo.getSort());
        webArticle.setShowType(webArticleVo.getShowType());
        webArticle.setUserId(currentUser.getId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webArticle);
        }else{
            num=insert(webArticle);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }else{
            webTagContentService.delete(new WebTagContent("",webArticle.getId()));
            if(StrUtil.isNotEmpty(webArticleVo.getTagIds())){
                String[] list=webArticleVo.getTagIds().split(",");
                if(list!=null&&list.length>0){
                    for(String id:list){
                        webTagContentService.insert(new WebTagContent(id, WebBaseContent.web_tag_type_wz,webArticle.getId(),currentUser));
                    }
                }
            }
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-5 21:56:08
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebArticle webArticle=selectByPrimaryKey(id);
        if(webArticle!=null){
            webArticle.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webArticle.setUpdateTime(new Date());
            webArticle.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webArticle);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }
    /**
     * 通过标签id获取所属目录
     *
     * @param webArticleVo 对象
     * @author shengQiang yu
     * @date 2019-04-02 12:42
     */
    public List<WebArticle> listLeftByTagId(WebArticleVo webArticleVo){
        return webArticleDao.listLeftByTagId(webArticleVo);
    }
    /**
     * 获取集合
     *
     * @param webArticleVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-6-5 21:56:08
     */
    public List<WebArticle> acceptList(WebArticleVo webArticleVo){
        Example example = new Example(WebArticle.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webArticleVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webArticleVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(webArticleVo.getColId())){
            criteria.andEqualTo("colId",webArticleVo.getColId());
        }
        if(StringUtil.isNotEmpty(webArticleVo.getTitle())){
            criteria.andLike("title","%"+webArticleVo.getTitle()+"%");
        }
        if(StringUtil.isNotEmpty(webArticleVo.getAuthor())){
            criteria.andLike("author","%"+webArticleVo.getAuthor()+"%");
        }
        if(StringUtil.isNotEmpty(webArticleVo.getShowType())){
            criteria.andEqualTo("showType",webArticleVo.getShowType());
        }
        if(StringUtil.isNotEmpty(webArticleVo.getUserId())){
            criteria.andEqualTo("userId",webArticleVo.getUserId());
        }
        example.setOrderByClause(" sort desc,update_time desc");
        return webArticleDao.selectByExample(example);
    }
}
